import {UIBase} from "./uibase.js";
import {UIPager} from "./uipager.js";
import {gid} from "./gid.js";

export class Album extends UIBase{

  constructor(props){
    super(props);
    this.pager = new UIBase();

    //分类map
    this.typeMap = new Map();

    //相册map结构：new Map(typeId, new Map(photo.id, photo))
    //默认相册map
    this.defaultTypeMap = new Map();
    //自定义相册map
    this.photoTypeMap = new Map();
  }

  addType(name){

    let id = gid();
    this.typeMap.set(id, name);
    this.photoTypeMap.set(id, new Map());
    this.render();
  }

  renameType(id, name){
    this.typeMap.set(id,name);
    this.render();
  }

  deleteType(){
    this.typeMap.delete(id);
    this.photoTypeMap.delete(id);
    this.render();
  }

  _getTypeIndex(typeId){
    let idsI = this.typeMap.keys();
    let ids = Array.from(idsI);
    for(let i =0;i< ids.length; i++){
      let id = ids[i];
      if (id === typeId) {
        return i;
      }
    }
    return -1;
  }

  //把一个type向上移动
  upType(typeId){

    //[[id,typeName]...]
    let index = this._getTypeIndex(typeId);
    if (index !== 0 && index !== -1) {

      let typeName = this.typeMap.get(typeId);
      this.typeMap.delete(typeId);

      let arr = [...this.typeMap];//通过这种形式将原本非数组形式变为数组
      let arrBefore = arr.slice(0,index - 1);
      let arrAfter arr.slice(index-1);
      arr = [...arrBefore,[TypeId,typeName],...arrAfter];

      //根据arr重新生成this.photoTypeMap
      this.typeMap = new Map(arr);
    }
    this.render();
  }

  downType(typeId){
    let index = this._getTypeIndex(typeId);
    if (index !== this.typeMap.size - 1 && index !== -1) {
      let typeName = this.typeMap.get(typeId);
      this.typeMap.delete(typeId);

      let arr = [...this.typeMap];
      let arrBefore = arr.slice(0,index+1);
      let arrAfter = arr.slice(index+1);
      arr = [...arrBefore,[typeId,typeName],...arrAfter];

      //根据arr重新生成this.typeMap
      this.typeMap = new Map(arr);
    }
    this.render();
  }

  merge(typeId1,typeId2){
    let photoMap1 = this.photoTypeMap.get(typeId1);
    let photoMap2 = this.photoTypeMap.get(typeId2);
    let newPhotoMap = new Map([...photoMap1,...photoMap2]);
    this.photoTypeMap.set(typeId1,newPhotoMap);

    this.typeMap.delete(typeId2);
    this.photoTypeMap.delete(typeId2);

    this.render();
  }

  addphoto(photoId){
    const photoMap = this.photoTypeMap.get(typeId);
    photoMap.set(photo.id, photo);
    this.render();
  }

  deletePhoto(){
    //遍历相册
    for (let [typeId,photoMap] of this.photoTypeMap){
      //若在相册中找到photoId则删除
      if (photoMap.has(photoId)) {
        photoMap.delete(photoId);
      }
    }
    this.render();
  }

  //将照片移动到新的类型中去
  moveToType(targetTypeId,photoId){
    //保存待移动照片的临时变量
    //let tempPhoto = new Map();

    //遍历相册
    for (let [typeId, photoMap] of this.photoTypeMap){
      //若找到photoId，将照片存入临时变量，并删除原照片
      let photo = null;

      if (photo = photoMap.delete(photoId)) {
        //已再判断条件中删除，不必再删除一次
        //photoMap.delete(photoId);

        this.photoTypeMap.get(targetTypeId).set(photo.id,photo);
        break;
      }
    }
    this.render();
  }

  _getPhotoIndex(photoId){
    for (let [typeId, photoMap] of this.photoTypeMap){
      if (photoMap.has(photoId)) {
        let idsI = photoMap.keys();
        let ids = Array.from(idsI);
        for(let i = 0; i< ids.length;i++){
          if (ids[i] === photoId) {
            return i;
          }
        }
        return -1;
      }
    }
  }

  upPhoto(id){
    let index = this._getPhotoIndex(id);
    if (index && index !== 0 && index !== -1) {
      for(let [typeId, photoMap] of this.photoTypeMap){
        if (photoMap.has(id)) {
          let photo = photoMap.get(id);
          photoMap.delete(id);

          let arr = [...photoMap];
          let arrBefore = arr.slice(0,index -1);
          let arrAfter = arr.slice(index-1);
          arr = [...arrBefore,[id,photo], ...arrAfter];

          //根据arr 根据arr重新生成photoMap
          photoMap = new Map(arr);
          this.photoTypeMap.set(typeId, photoMap);
        }
      }
    }
    this.render();

  }

  downPhoto(id){
    let index = this._getPhotoIndex(id);
    if (index && index !== -1) {
      for(let [typeId,photoMap] of this.photoTypeMap){
        if(photoMap.has(id) && index !== photoMap.size - 1){
          let photo = photoMap.get(id);
          photoMap.delete(id);

          let arr = [...photoMap];
          let arrBefore = arr.slice(0,index+1);
          let arrAfter = arr.slice(index +1);
          arr = [...arrBefore,[id,photo], ...arrAfter];

          //根据arr重新生成photoMap
          photoMap = new Map(arr);
          this.photoTypeMap.set(typeId,photoMap);
        }
      }
    }
    this.render();
  }

  renamePhoto(id, name){
    //遍历相册
    for (let [typeId, photoMap] of this.photoTypeMap){
      //若找到photo则将其重命名
      let photo;
      if (photo = photoMap.get(id)) {
        photo.name = name;
        break;
      }
    }
    this.render();
  }

  _render(){

    return`
      <div class="album">
      <nav class="menu pl10">
        <a href="#">新建相册</a>
        <a href="#">添加照片</a>
      </nav>
      <div class="content">
        <nav class="type-list">
          <a href="#" data-type="default">默认相册</a>
        </nav>
        <div class="photo-list">
          ${photo}
        </div>
      </div>
      </div>
    `;
  }

  _bindEvent(element){
    element.querySelector(".photo-list").appendChild(this.pager.element);
  }
}
